package com.heima.admin.controller.v1;

import com.heima.admin.service.ChannelService;
import com.heima.model.admin.dto.ChannelDto;
import com.heima.model.admin.pojo.AdChannel;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.common.validator.ValidateAdd;
import com.heima.model.common.validator.ValidateUpdate;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(value = "ChannelController",tags = "频道管理API")
@RestController//controller + responsebody,封装返回json
@RequestMapping("/api/v1/channel")
public class ChannelController {

    @Autowired
    ChannelService channelService;

    @ApiOperation("根据频道名称分页查询")
    @PostMapping("/list")
    public ResponseResult list(@RequestBody ChannelDto channelDto){

        ResponseResult responseResult = channelService.findByNameAndPage(channelDto);

        return responseResult;

    }

    @ApiOperation("查询频道类别")
    @GetMapping("/channels")
    public ResponseResult findAll(){

        List<AdChannel> list = channelService.list();//查询所有

        return ResponseResult.okResult(list);

    }
    @ApiOperation("频道新增")
    @PostMapping("/save")
    public ResponseResult save(@RequestBody @Validated(ValidateAdd.class) AdChannel adChannel){//前端传json,后端用实体类来接收    RequestBody

        return channelService.insert(adChannel);

    }


    @ApiOperation("频道修改")
    @PostMapping("/update")
    public ResponseResult update(@RequestBody @Validated(ValidateUpdate.class) AdChannel adChannel){//前端传json,后端用实体类来接收    RequestBody

        if (adChannel.getId()==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"id为空");
        }
        //id不能为空,使用条件
        boolean b = channelService.updateById(adChannel);
        return ResponseResult.okResult();

    }
    @ApiOperation("频道删除")
    @GetMapping("/del/{id}")//占位符
    public ResponseResult delete(@PathVariable Integer id){//路径传参

        return channelService.delete(id);
    }


}
